//
//  SuperviseTaskRecordItemView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/10.
//

import Foundation
import UIKit

class SuperviseTaskRecordItemView: UIScrollView {
    let basicV = UIView.createBase()
    let nameL = UILabel()
    let resultItem = InspListDetailItem(withKeyWidth: 100)    // 检查结论
    let dateItem = InspListDetailItem(withKeyWidth: 100)  // 检查时间

    let imgBV = UIView.createBase()
    let collectionV = SEImageCollectionView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .bg
        showsVerticalScrollIndicator = false
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: SpRwbgtzZgjlModal) {
        nameL.text = modal.tzsm
        resultItem.update(withKey: "检查结论", value: modal.rwztmc)
        dateItem.update(withKey: "检查时间", value: modal.tjsj)
        
        if modal.rwztmc == "不合格" {
            resultItem.valueL.textColor = .hex("#FF3203")
        } else if modal.rwztmc == "合格" {
            resultItem.valueL.textColor = .hex("#66BC60")
        }
        
        var imgs: [String] = []
        let images = modal.zgfjs ?? []
        for img in images {
            if img.fjdz != nil {
                imgs.append(img.fjdz!)
            }
        }
        collectionV.imgs = imgs
    }
    
    func setupBasicUI() {
        addSubview(basicV)
        basicV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        nameL.numberOfLines = 0
        nameL.font = .systemFont(ofSize: 15)
        basicV.addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
        }
        
        let line = UIView()
        line.backgroundColor = .hex("#EFEFEF")
        basicV.addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(nameL.snp.bottom).offset(26)
            make.left.equalTo(nameL.snp.left)
            make.right.equalTo(nameL.snp.right)
            make.height.equalTo(0.5)
        }

        basicV.addSubview(resultItem)
        resultItem.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(line.snp.bottom).offset(10)
            make.height.equalTo(40)
        }
        
        basicV.addSubview(dateItem)
        dateItem.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(resultItem.snp.bottom)
            make.height.equalTo(40)
            make.bottom.equalToSuperview().offset(-12)
        }
    }
    
    func setupUI() {
        // 基本信息
        setupBasicUI()
        
        // 整改记录
        addSubview(imgBV)
        imgBV.snp.makeConstraints { make in
            make.top.equalTo(basicV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalTo(self.snp.bottom).offset(-40)
        }
        
        let imgTV = TitleItemView(withTitle: "现场图像", hasIcon: false)
        imgBV.addSubview(imgTV)
        imgTV.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(46)
        }
        
        let line = UIView()
        line.backgroundColor = .hex("#EFEFEF")
        imgBV.addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(imgTV.snp.bottom)
            make.left.equalToSuperview().offset(11)
            make.right.equalToSuperview().offset(-13)
            make.height.equalTo(0.5)
        }
        
        collectionV.itemHeight = 70
        collectionV.maxCount = 4
        imgBV.addSubview(collectionV)
        collectionV.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().offset(-13)
            make.top.equalTo(line.snp.bottom).offset(16)
        }
    }
}
